package oyl.selenium.config.translate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.volcengine.model.request.LangDetectRequest;
import com.volcengine.model.request.TranslateTextRequest;
import com.volcengine.model.response.LangDetectResponse;
import com.volcengine.model.response.TranslateTextResponse;
import com.volcengine.service.translate.ITranslateService;
import com.volcengine.service.translate.impl.TranslateServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

public class DouyinTranslateDemo {
    public static   ITranslateService translateService = TranslateServiceImpl.getInstance();

    static  {
        translateService.setAccessKey("AKLTOTg5NTBkYjBiZWU1NGU0Y2E1YmE0YzEwMGU1MDE2OTQ");
        translateService.setSecretKey("WlRnME9HVTNObVpqTWpKaE5ESTFaRGczTm1ZMFl6TmhPVFkzWVRneFlqWQ==");
    }


    public static void main(String[] args) throws Exception {

        System.out.println(translateText("en","zh",
                "< br ><br><br>"
                ));
    }

    public static String translateText(String text, String sourceLang, String targetLang) throws Exception{
        // translate text
        try {
            TranslateTextRequest translateTextRequest = new TranslateTextRequest();
             translateTextRequest.setSourceLanguage(sourceLang); // 不设置表示自动检测
            translateTextRequest.setTargetLanguage(targetLang);
            translateTextRequest.setTextList(Arrays.asList(text));

            TranslateTextResponse translateText = translateService.translateText(translateTextRequest);
            if(CollectionUtils.isEmpty(translateText.getTranslationList())){
                throw new RuntimeException(JSONObject.toJSONString(translateText));
            }
            String res=  translateText.getTranslationList().get(0).getTranslation();
            res= res.replaceAll(" - ","-").replaceAll("-sv-","-SV-");
            //System.out.println(res);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}